package com.sk.ccflower.lib.commonUtils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil{

/**
     * 1초 = 1000
     */
    public static int MSEC=1000;
/**
     * 1분 = 1000 * 60 으로 정의
     */
    public static int MIN_MSEC=DateUtil.MSEC*60;

/**
     * 1시간 = 1000 * 60 * 60
     */
    public static int HOUR_MSEC=DateUtil.MIN_MSEC*60;
/**
     * 1일 = 1000 * 60 * 60 * 24
     */
    public static long DAY_MSEC=DateUtil.HOUR_MSEC*24;

    public static long MONTH_MSEC = DateUtil.DAY_MSEC * 30;

    public static String getCurrentTime(){
        return getCurrentTime("yyyyMMddHHmmss");
    }

    public static String getCurrentTime( String format ){

        SimpleDateFormat formatter=new SimpleDateFormat( format , Locale.KOREA );

        String date=null;
        try{
            date=formatter.format( new Date() );
        } catch( Exception ex ){
        }
        //if( format.indexOf("/") >= 0 ) date = date.replaceAll("/","");
        return date;
    }

    public static int getTodayDayOfWeek(){
        return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
    }

    public static int getCurrentUnixTime(){

        Date date=new Date();

        return( int )( date.getTime()/1000 );
    }

    public static String getBeforeTime( String format , int whatTime ){
        return getTime( format , whatTime , -1 );
    }

    public static String getBeforeTime( String format , int whatTime , int amount ){
        return getTime( format , whatTime , -amount );
    }

    public static String getNextTime( String format , int whatTime ){
        return getTime( format , whatTime , 1 );
    }

    public static String getNextTime( String format , int whatTime , int amount ){
        return getTime( format , whatTime , amount );
    }

    public static String getSpecificNextTime( String format , long unixTime , int whatTime , int amount ){
        return getSpecificTime( format , unixTime , whatTime , amount );
    }

    private static String getTime( String format , int whatTime , int amount ){
        SimpleDateFormat formatter=new SimpleDateFormat( format , Locale.KOREA );
        String nextDate=null;
        Calendar cal=Calendar.getInstance( Locale.KOREA );
        cal.add( whatTime , amount );
        try{
            nextDate=formatter.format( cal.getTime() );
        } catch( Exception ex ){
        }
        return nextDate;
    }

    private static String getSpecificTime( String format , long unixTime , int whatTime , int amount ){
        SimpleDateFormat formatter=new SimpleDateFormat( format , Locale.KOREA );
        String nextDate=null;
        Calendar cal=Calendar.getInstance( Locale.KOREA );
        cal.setTime( new Date( unixTime*1000 ) );
        cal.add( whatTime , amount );
        try{
            nextDate=formatter.format( cal.getTime() );
        } catch( Exception ex ){
        }
        return nextDate;
    }

    public static String changeFormat( String new_format , String yyyymmddhhmmss ){
                long time=DateUtil.getUnixTime( "yyyyMMddHHmmss", yyyymmddhhmmss );
                return DateUtil.unixTimeToDateFormat( time , new_format);
    }

    public static boolean isTimeFormat( String format , String yyyymmddhhmmss ){
        boolean result = false;
        SimpleDateFormat sdf=new SimpleDateFormat();
        sdf.applyPattern( format );
        Calendar cal=sdf.getCalendar();
        long time=DateUtil.getUnixTime( format , yyyymmddhhmmss );
        if( time == -1 ) result = false;

        String temp = DateUtil.unixTimeToDateFormat(time , format);
        if( temp.equals(yyyymmddhhmmss) ){
            result = true;
        }else{
            result = false;
        }

        return result;
    }

    public static void main(String[] args){
        String aaa= DateUtil.changeFormat("yyyyMMddHH" , "20081106123030");

        System.out.println( DateUtil.isTimeFormat("yyyyMMddHHmmss" , "20081206123090") );

    }

    public static int getUnixTime( String pattern , String timeValue ){
        SimpleDateFormat formatter=new SimpleDateFormat( pattern , Locale.ENGLISH );

        Date date=null;
        try{
            date=formatter.parse( timeValue );
        } catch( ParseException ex ){
            //ex.printStackTrace();
            return-1;
        }
        return( int )( date.getTime()/1000 );
    }

    /**
     * 주기 시각으로 변환
     * @param _unixTime			변환 대상 시각
     * @param unitSec			주기 (단위: sec)
     * @return
     */
    public static long getFixedUnixTime (long _unixTime, long unitSec )
    {
        return DateUtil.getUnixTime("yyyyMMddHHmmss", DateUtil.getFixedTime (_unixTime, unitSec));
    }

    public static String getFixedTime (long _unixTime, long unitSec )
    {
        return DateUtil.getFixedTime (unixTimeToDateFormat(_unixTime, "yyyyMMddHHmmss"), unitSec );
    }

    /**
     * 주기 시각으로 변환
     * @param _dt			변환 대상 시각
     * @param unitSec		주기 (단위: sec)
     * @return
     */
    public static String getFixedTime (String _dt, long unitSec ){
        long fixedTime = unitSec * 1000;
        if( ( fixedTime<DateUtil.MIN_MSEC )||( fixedTime>DateUtil.MONTH_MSEC ) )
            return null;

        String fixedTimeStr="";
        String headTime="" , bodyTime="";

        if( ( fixedTime>=DateUtil.MIN_MSEC )&&( fixedTime<DateUtil.HOUR_MSEC ) ){
            headTime=DateUtil.unixTimeToDateFormat(DateUtil.getUnixTime("yyyyMMddHHmmss", _dt), "yyyyMMddHH");
            int getMinTime=Integer.parseInt( DateUtil.unixTimeToDateFormat(DateUtil.getUnixTime("yyyyMMddHHmmss", _dt), "m"));
            bodyTime=""+( getMinTime/( fixedTime/DateUtil.MIN_MSEC ) )*( fixedTime/DateUtil.MIN_MSEC );
            if( bodyTime.length()==1 )
                bodyTime="0"+bodyTime;
        } else if( ( fixedTime>=DateUtil.HOUR_MSEC )&&( fixedTime<DateUtil.MONTH_MSEC ) ){
            headTime=DateUtil.unixTimeToDateFormat(DateUtil.getUnixTime("yyyyMMddHHmmss", _dt), "yyyyMMdd");
            int getHourTime=Integer.parseInt(DateUtil.unixTimeToDateFormat(DateUtil.getUnixTime("yyyyMMddHHmmss", _dt), "HH"));
            bodyTime=""+( getHourTime/( fixedTime/DateUtil.HOUR_MSEC ) )*( fixedTime/DateUtil.HOUR_MSEC );
            if( bodyTime.length()==1 )
                bodyTime="0"+bodyTime;
            bodyTime += "00";
        } /*else if( ( fixedTime>=DateUtil.DAY_MSEC )&&( fixedTime<DateUtil.MONTH_MSEC ) ){
            headTime = DateUtil.getCurrentTime("yyyyMM");
            int getDayTime = Integer.parseInt( DateUtil.getCurrentTime("dd"));
            bodyTime = "" + ( getDayTime/( fixedTime/DateUtil.DAY_MSEC ) )*( fixedTime/DateUtil.DAY_MSEC );
            if( bodyTime.length() == 1 ) bodyTime = "0"+ bodyTime;
            bodyTime += "00";
        }*/
        else{ /* nothing */}

        fixedTimeStr=headTime+bodyTime + "00";
        return fixedTimeStr;
    }


    public static String getFixedTime( long unitSec ){
        long fixedTime = unitSec * 1000;
        if( ( fixedTime<DateUtil.MIN_MSEC )||( fixedTime>DateUtil.MONTH_MSEC ) )
            return null;

        String fixedTimeStr="";
        String headTime="" , bodyTime="";

        if( ( fixedTime>=DateUtil.MIN_MSEC )&&( fixedTime<DateUtil.HOUR_MSEC ) ){
            headTime=DateUtil.getCurrentTime( "yyyyMMddHH" );
            int getMinTime=Integer.parseInt( DateUtil.getCurrentTime( "m" ) );
            bodyTime=""+( getMinTime/( fixedTime/DateUtil.MIN_MSEC ) )*( fixedTime/DateUtil.MIN_MSEC );
            if( bodyTime.length()==1 )
                bodyTime="0"+bodyTime;
        } else if( ( fixedTime>=DateUtil.HOUR_MSEC )&&( fixedTime<DateUtil.MONTH_MSEC ) ){
            headTime=DateUtil.getCurrentTime( "yyyyMMdd" );
            int getHourTime=Integer.parseInt( DateUtil.getCurrentTime( "HH" ) );
            bodyTime=""+( getHourTime/( fixedTime/DateUtil.HOUR_MSEC ) )*( fixedTime/DateUtil.HOUR_MSEC );
            if( bodyTime.length()==1 )
                bodyTime="0"+bodyTime;
            bodyTime += "00";
        } /*else if( ( fixedTime>=DateUtil.DAY_MSEC )&&( fixedTime<DateUtil.MONTH_MSEC ) ){
            headTime = DateUtil.getCurrentTime("yyyyMM");
            int getDayTime = Integer.parseInt( DateUtil.getCurrentTime("dd"));
            bodyTime = "" + ( getDayTime/( fixedTime/DateUtil.DAY_MSEC ) )*( fixedTime/DateUtil.DAY_MSEC );
            if( bodyTime.length() == 1 ) bodyTime = "0"+ bodyTime;
            bodyTime += "00";
        }*/
        else{ /* nothing */}

        fixedTimeStr=headTime+bodyTime + "00";
        return fixedTimeStr;
    }



    public static int getDiffDate( String fromDate , String ToDate ){
        long from=getUnixTime( "yyyyMMddHHmmss" , fromDate+"000000" );
        long to=getUnixTime( "yyyyMMddHHmmss" , ToDate+"000000" );

        return( int )( to-from )/( 24*60*60 );
    }

    public static long getDiffTime(String _from, String _to){
        long from=getUnixTime( "yyyyMMddHHmmss", _from);
        long to=getUnixTime( "yyyyMMddHHmmss", _to);

        return to >= from ? (to - from):0;
    }

    public static String getSpecificBeforeDay( String format , String date , int beforeDay ){
        SimpleDateFormat formatter=new SimpleDateFormat( format , Locale.KOREA );
        return formatter.format( new Date( ((long)DateUtil.getUnixTime( "yyyyMMddHHmmss" , date+"000000" )-(long)( beforeDay*24*60*60 ))*1000 ) );
    }

    public static String getSpecificDayOfWeek( String yyyyMMddHHmmss ){
        SimpleDateFormat formatter=new SimpleDateFormat( "EE" , Locale.KOREA );
        return formatter.format( new Date( DateUtil.getUnixTime( "yyyyMMddHHmmss" , yyyyMMddHHmmss )*1000 ) );
    }

    public static String getSpecificWeek( String yyyyMMddHHmmss ){
        SimpleDateFormat formatter=new SimpleDateFormat( "yyyyww" , Locale.KOREA );
        String week=null;
        try{
            week=formatter.format( new Date( DateUtil.getUnixTime( "yyyyMMddHHmmss" , yyyyMMddHHmmss )*1000 ) );
        } catch( Exception ex ){
        }
        return week;
    }

    public static String unixTimeToDateFormat( long unixTime , String format ){
        SimpleDateFormat formatter=new SimpleDateFormat( format , Locale.KOREA );
        return formatter.format( new Date( unixTime*1000 ) );
    }

    public static String isDateValidate( String timeValue , String pattern ){
        if( timeValue==null )
            return " ";
        if( timeValue.length()==12 )
            timeValue+="00";
        SimpleDateFormat formatter=new SimpleDateFormat( pattern , Locale.ENGLISH );

        try{
            Date date=formatter.parse( timeValue );
            int unixTime=( int )( date.getTime()/1000 );
            if( DateUtil.unixTimeToDateFormat( unixTime , pattern ).equals( timeValue ) ){
                return timeValue;
            } else{
                return DateUtil.getCurrentTime( pattern );
            }

        } catch( ParseException ex ){
            ex.printStackTrace();
            return DateUtil.getCurrentTime( pattern );
        }
    }

    /**
     * 주어진 날짜의 이전 요일 날짜 찾음
     * @param _dt				날짜
     * @param _dayOfWeek		요일, 1:일, 2:월, 3:화...
     * @return
     */
    public static String getDateForDayOfWeek(String _dt, int _dayOfWeek)
    {
        long millSec = (long)DateUtil.getUnixTime("yyyyMMddHHmmss", _dt) * 1000;

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(millSec);

        int week = cal.get(Calendar.DAY_OF_WEEK);
        int diff = ((week - _dayOfWeek) +7) %7;

        return DateUtil.getSpecificBeforeDay("yyyyMMddHHmmss", _dt, diff);
    }
    
    public static String getTimeStr( Date date, String format ) {
		if ( null == date )	return null;
		
		SimpleDateFormat formatter = new SimpleDateFormat( format, Locale.KOREA );
		return formatter.format( date );
	}
    
}
